// ==UserScript==
// @name          mixiCommentEraser
// @namespace     http://d.hatena.ne.jp/zorio/
// @description   This script hides selected users' comments in mixi BBS
// @include       http://mixi.jp/view_bbs.pl*
// @include       http://mixi.jp/view_diary.pl*
// @include       http://mixi.jp/view_event.pl*
// @include       http://mixi.jp/view_enquete.pl*
// ==/UserScript==

var st = new Date()
var UserList = {
	list: {}, 
	users: {}, 
	load: function() {
		var l = this.list = eval(GM_getValue('list')) || {};
		//GM_log(l.toSource());
		var u = {}
		for (var code in l) {
			var name = l[code];
			u[code] = new User(code, name);
		}
		this.users = u;
	}, 
	add: function(code, name) {
		this.list[code] = name;
		this._save();
		this.load();
		this.parse(false);
	}, 
	remove: function(code) {
		var l = {};
		var user = this.users[code];
		user.show();
		this.users[code] = undefined;
		for (var c in this.list) {
			if (c !== code) {
				l[c] = this.list[c];
			}
		}
		this.list = l;
		this._save();
		this.showList();
	}, 
	parse: function(first) {
		var as = document.evaluate(
			'//dl[contains(@class, "comment")]//a[contains(@href, "show_friend.pl?id=")]', 
			document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
		for (var i = 0, elem; elem = as.snapshotItem(i); i++) {
			var code = elem.href.substring(33);// 33: "http://mixi.jp/show_friend.pl?id=".length
			var name = elem.text;
			var user = this.users[code];
			if (user && name) {
				user.add(elem);
			}
			if (first) this._addButton(elem, code, name);
		}
		
		var users = this.users;
		for (var code in users) {
			var user = users[code];
			user.hide();
		}
		this.showList();
	}, 
	showList: function() {
		var id = "GM_hiddenList";
		var div = document.getElementById(id);
		if (!div) {
			var anc = document.createElement("div");
			with (anc.style) {
				backgroundColor = "white";
				position = "fixed";
				margin = "5px";
				padding = "5px";
				bottom = "0px";
				right = "0px";
				fontWeight = "bold";
				border = "solid 2px #ff9933";
			}
			anc.innerHTML = "\u7121\u8996\u30ea\u30b9\u30c8";
			document.body.appendChild(anc);
			div = document.createElement("div");
			div.id = id;
			with (div.style) {
				backgroundColor = "white";
				position = "fixed";
				margin = "5px";
				padding = "5px";
				bottom = "20px";
				right = "20px";
				display = "none";
				border = "solid 1px #ff9933";
			}
			document.body.appendChild(div);
			var show = function(){div.style.display=""};
			var hide = function(){div.style.display="none"};
			anc.addEventListener("mouseover", show, false);
			anc.addEventListener("mouseout", hide, false);
			div.addEventListener("mouseover", show, false);
			div.addEventListener("mouseout", hide, false);
		}
		var tbl = document.createElement("table");
		tbl.appendChild(document.createElement("thead"));
		var r = tbl.tHead.insertRow(0);
		r.insertCell(0).appendChild(document.createTextNode("\u540d\u524d"));
		r.insertCell(1).appendChild(document.createTextNode("\u4ef6\u6570"));
		r.insertCell(2).appendChild(document.createTextNode(""));
		r.style.backgroundColor = "#fff4e0";
		tbl.appendChild(document.createElement("tbody"));
		for (var code in this.users) {
			var user = this.users[code];
			if (user) {
				r = tbl.tBodies[0].insertRow(tbl.tBodies[0].rows.length);
				r.insertCell(0).appendChild(document.createTextNode(user.name));
				r.insertCell(1).appendChild(document.createTextNode(user.list.length));
				this._removeButton(r.insertCell(2), code);
			}
		}
		div.innerHTML = '';
		div.appendChild(tbl);
	}, 
	_save: function() {
		GM_setValue('list', this.list.toSource());
		//GM_log(this.list.toSource());
	}, 
	_addButton: function(elem, code, name) {
		var self = this;
		var btn = document.createElement("input");
		btn.style.marginLeft = "10px";
		btn.type = "button";
		btn.value = "\u7121\u8996\u30ea\u30b9\u30c8\u8ffd\u52a0";
		btn.addEventListener("click", function(){ self.add(code, name)}, false);
		//btn.addEventListener("click", function(){ GM_log(code+":"+name);}, false);
		elem.parentNode.appendChild(btn);
	}, 
	_removeButton: function(elem, code) {
		var self = this;
		var btn = document.createElement("input");
		btn.type = "button";
		btn.value = "\u9664\u5916";
		btn.addEventListener("click", function(){ self.remove(code)}, false);
		elem.appendChild(btn);
	}
}

var User = function() {
	this.initialize.apply(this, arguments);
}

User.prototype = {
	initialize: function(code, name) {
		this.code = code;
		this.name = name;
		this.list = [];
	}, 
	add: function(a) {
		this.list.push(a);
	}, 
	show: function() {
		this.change("");
	}, 
	hide: function() {
		this.change("none");
	}, 
	change: function(style) {
		for (var i = 0, im = this.list.length; i < im; i++) {
			var elem = this.list[i];
			var targetNode = elem;
			while (targetNode) {
				// in view_diary.pl, dl.className is "commentList01"
				// in view_bbs.pl, view_event.pl, and view_enquete.pl, dl.className is "commentContent01"
				if (targetNode.nodeName === "DL" && targetNode.className) {
					var cn = targetNode.className;
					if (cn === "commentContent01" || cn === "commentList01") {
						targetNode.style.display = style;
						break;
					}
				}
				targetNode = targetNode.parentNode;
			}
		}
	}
}

UserList.load();
UserList.parse(true);
var en = new Date()
//GM_log(en - st);
